iT邦幫忙

2023 iThome 鐵人賽

DAY 2
0

This series of tutorials is aimed to share the notes taken while I was learning python for cybersecurity with the books - Black Hat Python.
這系列教學文章為學習筆記+延伸資源,旨在分享學習書籍 Black Hat Python時所思所學,也希望能幫助想了解Python和資安的大大們入門。

This tutorail has also been written in English in Medium.

目錄

  • Proxy VII

看文前, 你應該要具備以下基礎能力:


Let's get started! 開始吧!


Proxy VII

Whole code

def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        server.bind((local_host, local_port))
    except Exception as e:
        print("[!!] Failed to listen on %s:%d" % (local_host, local_port))
        print("[!!] Check for other listening sockets or correct permissions.")
        print(e)
        sys.exit(0)

    print("[*] Listening on %s:%d" % (local_host, local_port))
    server.listen(5)
    while True:
        client_socket, addr = server.accept()
        print("> Received incoming connection from %s:%d" % (addr[0], addr[1]))
        
        proxy_thread = threading.Thread(
            target=proxy_handler,
            args=(client_socket, remote_host,
                  remote_port, receive_first))
        proxy_thread.start()

1.

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

建立一個socket

2.

server.bind((local_host, local_port))

The socket binds to the local host and listens.
socket綁定在當地host並聆聽。

3.

while True:
        client_socket, addr = server.accept()
        print("> Received incoming connection from %s:%d" % (addr[0], addr[1]))
        
        proxy_thread = threading.Thread(
            target=proxy_handler,
            args=(client_socket, remote_host,
                  remote_port, receive_first))
        proxy_thread.start()

In the main loop, when a fresh connection request comes in, we hand it off to the proxy_handler in a raw thread, which does all the sending and receiving of juicy bits to either side of the data stream.

  • 在主要迴圈中,當一個新的連接請求進來,我們將它移轉到the proxy_handler的裸執行續(we hand it off to the proxy_handler in a raw thread),如此一來,就能做到在雙向資料流中所有傳送和接收的重要bits。

Reference參考資料

推薦影片
絕讚! Youtube 教學影片 | Elevate Cyber

原始碼
Github - Python For Cybersecurity | Monles


上一篇
Day 15 - Proxy VI
下一篇
Day 17 - Proxy VIII
系列文
為駭而生 - Python 18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言